//
//  MutableSelectController.m
//  tableview多选问题
//
//  Created by 华杨科技 on 2017/5/20.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "MutableSelectController.h"
#import "KLMianMiCell.h"

#define UI_View_Height [UIScreen mainScreen].bounds.size.height//屏幕高度
#define UI_View_Width [UIScreen mainScreen].bounds.size.width//屏幕宽度

// 点击cell block回调
typedef void(^ClickCellBlock)(NSString *content,NSMutableArray *array);


@interface MutableSelectController () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *selectedArray;

@property (nonatomic, copy)ClickCellBlock click;


@end

@implementation MutableSelectController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)selectedArray {
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupSubviews];
    
    self.click = ^(NSString *content, NSMutableArray *array) {
        NSLog(@"数据:%@ %@",content,array);
    };
    
    
    [self readJson];
}

#pragma mark - 读取数据
/**读取本地json文件*/
- (void)readJson{

    NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mianmiList" ofType:nil]];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *infoArr = dic[@"infor"];
    
    NSMutableArray *newArray = [NSMutableArray array];

    for (NSDictionary *dic in infoArr) {
        [newArray addObject:dic];
    }
    
    self.dataArray = newArray.mutableCopy;
    
    NSLog(@"%@",self.dataArray);
    
}

#pragma mark - 布局
- (void)setupSubviews {
    [KLHelp navSetMianMi:self doBackHandler:^(UIButton *backBtn) {
        [backBtn addTarget:self action:@selector(doBack:) forControlEvents:(UIControlEventTouchUpInside)];

    } saveHandler:^(UIButton *saveBtn) {
        [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:(UIControlEventTouchUpInside)];

    }];
    
    self.title = @"免密管理";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
}- (void)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveAction:(UIButton *)sender {
    
}


#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = [NSString stringWithFormat:@"identifier%zd",indexPath.section];
    
    KLMianMiCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[KLMianMiCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //    cell.titleLb.text = self.dataArray[indexPath.section];
    
    
    [self displayMianMiCell:cell dic:self.dataArray[indexPath.section]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    KLMianMiCell *cell = (KLMianMiCell *)[tableView cellForRowAtIndexPath:indexPath];

    // 更新点击状态:取反
    [cell UpdateCellSelected:!cell.selected];
    
    if (cell.isSelected) {
        
        //        [self.selectedArray addObject:self.dic];
        [self.selectedArray addObject:cell.titleLb.text];
        
    }else {
        
        //        for (NSDictionary *dic  in self.selectedArray) {
        //            if ([cell.operation_id isEqualToString:dic[@"operation_id"]]) {
        //                NSLog(@"%zd---%zd--:%@",(long)indexPath.section,indexPath.row,cell.operation_id);
        //
        //                [self.selectedArray removeObject:self.dic];
        //            }
        //        }
                [self.selectedArray removeObject:cell.titleLb.text];
    }
    
    self.click(cell.titleLb.text, self.selectedArray);
}
// 给cell 赋值
- (void)displayMianMiCell:(KLMianMiCell *)cell dic:(NSDictionary *)dic {
    NSString *titleStr = dic[@"description"];
    NSString *freepassword = dic[@"freepassword"];
    NSLog(@"是否免密:%@",dic[@"freepassword"]);
    
    BOOL isFreepassword = ([freepassword isKindOfClass:[NSNull class]]) ? YES : NO;
    
    cell.titleLb.text = titleStr;
    cell.isSelected = isFreepassword; // 是否免密
    cell.operation_id = dic[@"operation_id"]; // 这个要传给服务器，服务器就讲对应的operation_id 的 isFreepassword 设置为真。
}


@end
